/* rwroxr.p - ROUTING REPONSIBILITY OWNING PROGRAM                            */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.9 $                                                              */
/*                                                                            */
/* This routine provides the procedures which are executed as a result of the */
/* call from the appropriate Data Type Controllers / structured programs      */
/* This routine owns the business logic for Routing Records                   */
/*                                                                            */
/* Revision: 1.1       BY: G.Latha         DATE: 03/03/00   ECO: *N03T*       */
/* Revision: 1.2       BY: Anup Pereira    DATE: 06/01/00   ECO: *N0DP*       */
/* Revision: 1.6       BY: Anup Pereira    DATE: 07/31/00   ECO: *N0GY*       */
/* Revision: 1.7       BY: Murali Ayyagari DATE: 10/24/00   ECO: *N0V1*       */
/* Revision: 1.8       BY: Vandna Rohira   DATE: 10/19/01   ECO: *M1NH*       */
/* $Revision: 1.9 $       BY: Dipesh Bector   DATE: 04/08/02   ECO: *N1G8*       */

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*                                                                            */

/*V8:ConvertMode=NoConvert                                                    */

&GLOBAL-DEFINE ROUTING_CODE 'R'


/* ========================================================================== */
/* ******************************* DEFINITIONS ****************************** */
/* ========================================================================== */

/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{pxmaint.i}

/* Define Handles for the programs. */
{pxphdef.i gpcmxr}
{pxphdef.i gpcodxr}
{pxphdef.i rwopmxr}
{pxphdef.i rwwcxr}
/* End Define Handles for the programs. */


/* ========================================================================== */
/* ******************************* MAIN BLOCK ******************************* */
/* ========================================================================== */

/* ========================================================================== */
/* ******************************** FUNCTIONS ******************************* */
/* ========================================================================== */

/*============================================================================*/
FUNCTION validateServiceOpRouting RETURNS logical
   (input pRoutingId as character):
/*------------------------------------------------------------------------------
Purpose:      Validate if Routing entered belongs to SSM module
Exceptions:   NONE
Conditions:
        Pre:  Entry of a Routing Id
        Post: Validated Routing Id
Notes:
History:
------------------------------------------------------------------------------*/
   define variable returnData as logical no-undo.

   returnData = can-find (first ro_det where ro_fsm_type = "FSM"
                          and ro_routing = pRoutingId).
   return (returnData).
END FUNCTION. /* validateServiceOpRouting */

/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE validateRoutingCode:
/*-----------------------------------------------------------------------------
Purpose:      VALIDATE IF ROUTING EXISTS
Exceptions:   WARNING-RESULT
Conditions:
        Pre:  Entry of Routing Code
        Post: Validated Routing Code
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRoutingId as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if not can-find(first ro_det where ro_routing = pRoutingId)
      then do:
         /* 126 - ROUTING DOES NOT EXIST */
         {pxmsg.i &MSGNUM     = 126
                  &ERRORLEVEL = {&WARNING-RESULT}
         }
         return {&WARNING-RESULT}.
      end. /* then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateRoutingCode */

/*============================================================================*/
PROCEDURE validateServiceRouting:
/*---------------------------------------------------------------------------
Purpose:      Check if the Routing identifier is a valid SSM Routing
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post:
Notes:
History:
---------------------------------------------------------------------------*/
   define input parameter pRoutingId as character no-undo.

   define variable fieldNumber as integer no-undo.
   define variable reject      as logical no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      {gprun.i ""fsbomv.p""
               "(input  {&ROUTING_CODE},
                 input  """",
                 input  pRoutingId,
                 input  yes,
                 output fieldNumber,
                 output reject)"}

      if reject then return error {&APP-ERROR-RESULT}.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateServiceRouting */

/*============================================================================*/
PROCEDURE processCreate :
/*------------------------------------------------------------------------------
Purpose:      Creation of the Routing Operation Detail record
Exceptions:
Conditions:
                Pre:  NONE
        Post: ro_det(c), ro_det(w)
Notes:
History:
---------------------------------------------------------------------------*/
   define input parameter pRoutingId    as character no-undo.
   define input parameter pOperation    as integer   no-undo.
   define input parameter pStartDate    as date      no-undo.
   define input parameter pStdOperation as character no-undo.
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      create ro_det.
      assign
         ro_routing   = pRoutingId
         ro_op        = pOperation
         ro_start     = pStartDate
         ro_milestone = yes.

      if recid(ro_det) = -1 then.

      if pStdOperation <> ""
      then do:

         /* READ STANDARD OPERATION RECORD */
         {pxrun.i &PROC  = 'processRead' &PROGRAM = 'rwopmxr.p'
                   &HANDLE=ph_rwopmxr
                  &PARAM = "(input  pStdOperation,
                             buffer opm_mstr,
                             input  no,
                             input  no)"
                  &NOAPPERROR = true
                  &CATCHERROR = true}

         if return-value = {&SUCCESS-RESULT}
         then do:
            assign
               ro_std_op    = opm_std_op
               ro_desc      = opm_desc
               ro_wkctr     = opm_wkctr
               ro_mch       = opm_mch
               ro_tran_qty  = opm_tran_qty
               ro_milestone = opm_mile
               ro_sub_lead  = opm_sub_lead
               ro_setup     = opm_setup
               ro_run       = opm_run
               ro_move      = opm_move
               ro_yield_pct = opm_yld_pct
               ro_tool      = opm_tool
               ro_vend      = opm_vend
               ro_inv_val   = opm_inv_val
               ro_sub_cost  = opm_sub_cost.

            if opm_cmtindx <> 0
            then do:
               /* COPY THE TRANSACTION COMMENTS FROM STANDARD OPERATION */
               {pxrun.i &PROC = 'copyComments' &PROGRAM = 'gpcmxr.p'
                         &HANDLE=ph_gpcmxr
                        &PARAM = "(input  opm_cmtindx,
                                   output ro_cmtindx)"
                        &NOAPPERROR = true
                        &CATCHERROR = true}
            end. /* if opm_cmtindx <> 0 */

            /* READ WORK CENTER RECORD */
            {pxrun.i &PROC  = 'processRead' &PROGRAM = 'rwwcxr.p'
                      &HANDLE=ph_rwwcxr
                     &PARAM = "(input  opm_wkctr,
                                input  opm_mch,
                                buffer wc_mstr,
                                input  no,
                                input  no)"
                     &NOAPPERROR = true
                     &CATCHERROR = true}

            if return-value = {&SUCCESS-RESULT} then
               assign
                  ro_mch_op    = wc_mch_op
                  ro_queue     = wc_queue
                  ro_wait      = wc_wait
                  ro_men_mch   = wc_men_mch
                  ro_setup_men = wc_setup_men.

         end. /* if return-value = {&SUCCESS-RESULT} */
      end. /* if pStdOperation <> "" */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* processCreate */

/*============================================================================*/
PROCEDURE processRead :
/*------------------------------------------------------------------------------
Purpose:      Read the Routing Operation Detail record based on the
              Routing Code, Operation and Start Date
Exceptions:   RECORD-LOCKED, RECORD-NOT-FOUND
Conditions:
                Pre:  NONE
        Post: ro_det(r)
Notes:
History:
---------------------------------------------------------------------------*/
   define input parameter pRoutingId as character no-undo.
   define input parameter pOperation as integer   no-undo.
   define input parameter pStartDate as date      no-undo.
   define parameter buffer ro_det for ro_det.
   define input parameter pLockFlag  as logical   no-undo.
   define input parameter pWaitFlag  as logical   no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pLockFlag
      then do:
         if pWaitFlag then
            find ro_det where ro_routing = pRoutingId
            and ro_op = pOperation and ro_start = pStartDate
            exclusive-lock no-error.
         else
            find ro_det where ro_routing = pRoutingId
            and ro_op = pOperation and ro_start = pStartDate
            exclusive-lock no-error no-wait.

         if locked ro_det then
            return {&RECORD-LOCKED}.
      end. /* if pLockFlag then do: */
      else
         for first ro_det
            where ro_routing = pRoutingId
            and   ro_op      = pOperation
            and   ro_start   = pStartDate
            no-lock: end.

      if not available ro_det then
         return {&RECORD-NOT-FOUND}.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* processRead */

/*============================================================================*/
PROCEDURE processDelete :
/*------------------------------------------------------------------------------
Purpose:      Deletion of the Routing Operation and associated records
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post: cmt_det(d), qad_wkfl(d), ro_det(d)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRouting   as character no-undo.
   define input parameter pOperation as integer no-undo.
   define input parameter pCmtIndx   as integer no-undo.
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* CHECK WHETHER ROUTING HAS BEEN USED IN COST SIMULATION */
      find first sop_det
         where sop_routing = pRouting
         and   sop_op      = pOperation
         exclusive-lock no-error.

      if available sop_det
      and sop_cost    <> 0
      then do:
         /* MESSAGE #5402 - DELETE NOT ALLOWED, DETAIL COST RECORDS EXIST */
         {pxmsg.i
            &MSGNUM=5402
            &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         return error {&APP-ERROR-RESULT}.
      end. /* if available sop_det */

      /* DELETE COMMENTS */
      for each cmt_det exclusive-lock where cmt_indx = pCmtIndx :
         delete cmt_det.
      end. /* for each cmt_det */

      /* DELETE SIDE-FILE DATA */
      for each qad_wkfl exclusive-lock where
          qad_key1 = "ro_det" + string(pRouting, "x(18)")
                              + string(pOperation, "99999") :
          delete qad_wkfl.
      end. /* for each qad_wkfl */

      /* DELETE sop_det RECORDS WITH sop_cost = 0 */
      if available sop_det
      then
         delete sop_det.

      delete ro_det.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* processDelete */

/*============================================================================*/
PROCEDURE validateRoutingPO :
/*------------------------------------------------------------------------------
Purpose:      Validate the PO Id and the Line Number of the Routing Operation
Exceptions:   {WARNING-RESULT}
Conditions:
        Pre:  ro_det(r)
        Post: Validated PO Id and Line Number
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pOrderNbr  as character no-undo.
   define input parameter pOrderLine as integer   no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pOrderNbr <> ""
      then do:
         for first po_mstr
            fields(po_nbr)
            where po_nbr = pOrderNbr
            no-lock: end.

         if not available po_mstr
         then do:
            /* MESSAGE #343 - PURCHASE ORDER DOES NOT EXIST */
            {pxmsg.i &MSGNUM=343
                     &ERRORLEVEL={&WARNING-RESULT}
                     &FIELDNAME=""ro_po_nbr""
            }
            return {&WARNING-RESULT}.
         end. /* if not available po_mstr then do: */
         else do:
            for first pod_det
               fields(pod_nbr pod_line)
               where pod_nbr  = pOrderNbr
               and   pod_line = pOrderLine
               no-lock: end.

            if not available pod_det
            then do:
               /* MESSAGE #6011 - ORDER LINE DOES NOT EXIST */
               {pxmsg.i &MSGNUM=6011
                        &ERRORLEVEL={&WARNING-RESULT}
                        &FIELDNAME=""ro_po_line""
               }
               return {&WARNING-RESULT}.
            end. /* if not available pod_det then do: */
         end. /* else do: */
      end. /* if pOrderNbr <> "" then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateRoutingPO */

/*============================================================================*/
PROCEDURE validateWIPMaterial :
/*------------------------------------------------------------------------------
Purpose:      Validate the WIP Part of the Routing Operation
Exceptions:   WARNING-RESULT
Conditions:
        Pre:  ro_det(r)
        Post: Validated WIP Part
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pWIPPart as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pWIPPart <> "" and not can-find(pt_mstr where pt_part = pWIPPart)
      then do:
         /* MESSAGE #16 - ITEM NUMBER DOES NOT EXIST */
         {pxmsg.i &MSGNUM=16
                  &ERRORLEVEL={&WARNING-RESULT}
                  &FIELDNAME=""ro_wipmtl_part""
         }
         return {&WARNING-RESULT}.
      end. /* then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateWIPMaterial */

/*============================================================================*/
PROCEDURE calculateBatchRun :
/*------------------------------------------------------------------------------
Purpose:      Calculate the Run Time for the batch
Exceptions:   NONE
Conditions:
        Pre:  ro_det(r)
        Post: ro_det(u)
Notes:
History:
------------------------------------------------------------------------------*/
   define input  parameter pBatchQty    as decimal no-undo.
   define input  parameter pRunPerBatch as decimal no-undo.
   define output parameter pRun         as decimal no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      pRun = if pBatchQty = 0 then pRunPerBatch
                else pRunPerBatch / pBatchQty.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* calculateBatchRun */

/*============================================================================*/
PROCEDURE calculateCycleRate :
/*------------------------------------------------------------------------------
Purpose:      Calculate the Cycle Rate based on the Routing Operation Run Time
Exceptions:   NONE
Conditions:
        Pre:  ro_det(r)
        Post: ro_det(u)
Notes:
History:
------------------------------------------------------------------------------*/
   define input  parameter pRun  as decimal no-undo.
   define output parameter pRate as decimal no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      pRate = if pRun <> 0 then 1 / pRun
                 else 0.
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* calculateCycleRate */

/*============================================================================*/
PROCEDURE validateBatchTime :
/*------------------------------------------------------------------------------
Purpose:      Round the Run Time Per Batch in case of precision overflow
Exceptions:   NONE
Conditions:
        Pre:  ro_det(r)
        Post: Validated Run Time Per Batch
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRunPerBatch as decimal no-undo.
   define input parameter pRun         as decimal no-undo.
   define input parameter pBatchQty    as decimal no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pRunPerBatch <> (pRun * pBatchQty) and pBatchQty <> 0
      then do:

         /* MESSAGE #4608 - RUNTIME PER BATCH WILL BE ROUNDED (PRECISION */
         /* OVERFLOW) */
         {pxmsg.i &MSGNUM=4608
                  &ERRORLEVEL={&WARNING-RESULT}
         }

         /* MESSAGE #4609 - RUNTIME PER BATCH WILL BE STORED AS */
         {pxmsg.i &MSGNUM=4609
                  &ERRORLEVEL={&WARNING-RESULT}
                  &MSGARG1="string((pRun * pBatchQty),'>>>,>>>,>>9.9<<<<<<<<')"
                  &FIELDNAME=""runTimePerBatch""
         }

         return {&WARNING-RESULT}.
      end. /* if runtime_per_batch ... */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateBatchTime */

/*============================================================================*/
PROCEDURE validateDateRange :
/*------------------------------------------------------------------------------
Purpose:      Validate overlap of Date Range with existing Routing Operation
              based on the entered Start and End Date
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pStartDate as date no-undo.
   define input parameter pEndDate   as date no-undo.
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      define variable conflicts as logical no-undo.
      define buffer rodet for ro_det.

      conflicts = false.
      for each rodet
         fields(ro_routing ro_op ro_end ro_start)
         where rodet.ro_routing = ro_det.ro_routing
         and   rodet.ro_op      = ro_det.ro_op
         and   recid(ro_det)   <> recid(rodet)
         and   ((rodet.ro_end   = ? and pEndDate     = ?)
         or     (rodet.ro_start = ? and pStartDate   = ?)
         or     (rodet.ro_start = ? and rodet.ro_end = ?)
         or     (pStartDate     = ? and pEndDate     = ?)
         or     ((pStartDate   >= rodet.ro_start or rodet.ro_start = ?)
         and     (pStartDate   <= rodet.ro_end   or rodet.ro_end   = ?
                                                 or pStartDate     = ?))
         or     ((pStartDate   <= rodet.ro_end   or pStartDate     = ?
                                                 or rodet.ro_end   = ?)
         and     (pEndDate     >= rodet.ro_start or pEndDate       = ?
                                                 or rodet.ro_start = ?)))
         no-lock:
         conflicts = true.
         leave.
      end. /* for each rodet */

      if conflicts = true
      then do:
         /* MESSAGE #122 - DATE RANGES MAY NOT OVERLAP */
         {pxmsg.i &MSGNUM=122
                  &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         return error {&APP-ERROR-RESULT}.
      end.  /* if conflicts */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateDateRange */

/*============================================================================*/
PROCEDURE validateEnd :
/*------------------------------------------------------------------------------
Purpose:      Validate End Date of the Routing Operation
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post: Validated End Date
Notes:
History:
------------------------------------------------------------------------------*/
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if (ro_start <> ? and ro_end <> ?) then
         if ro_start > ro_end
         then do:
            /* MESSAGE #4 - START DATE MUST BE PRIOR TO END DATE */
            {pxmsg.i &MSGNUM=4
                     &ERRORLEVEL={&APP-ERROR-RESULT}
            }
            return error {&APP-ERROR-RESULT}.
         end. /* if ro_start > ro_end then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateEnd */

/*============================================================================*/
PROCEDURE validateRoutingCodeEntry :
/*------------------------------------------------------------------------------
Purpose:      Validate entry of Blank or SSM Routing
Exceptions:   NONE
Conditions:
        Pre:  Entry of the Routing Id
        Post: Validated Routing Id
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRoutingId  as character no-undo.
   define input parameter pSSMRouting as logical   no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      /* BLANK ROUTING CODE NOT ALLOWED */
      if pRoutingId = ""
      then do:
         /* MESSAGE #906 - BLANK ROUTING NOT ALLOWED */
         {pxmsg.i &MSGNUM=906
                  &ERRORLEVEL={&APP-ERROR-RESULT}
                  &MSGARG1=""routing""
                  &MSGARG2=""""
                  &MSGARG3=""""
                  &FIELDNAME=""ro_routing""
          }
         return error {&APP-ERROR-RESULT}.
      end. /* if input pRoutingId = "" then do: */

      if pSSMRouting
      then do:
         /* MESSAGE #7423 - THIS IS A SERVICE ROUTING, NOT A STANDARD ROUTING */
         {pxmsg.i &MSGNUM=7423
                  &ERRORLEVEL={&APP-ERROR-RESULT}
                  &FIELDNAME=""ro_routing""
          }
         return error {&APP-ERROR-RESULT}.
      end. /* if pSsmRouting then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateRoutingCode */

/*============================================================================*/
PROCEDURE calculateRunTime :
/*------------------------------------------------------------------------------
Purpose:      Calculate Run and Cycle Time based on Hourly Production Rate
Exceptions:   NONE
Conditions:
        Pre:  ro_det(r)
        Post: ro_det(u)
Notes:
History:
------------------------------------------------------------------------------*/
   define input  parameter pHourRate as decimal no-undo.
   define output parameter pRun      as decimal no-undo.
   define output parameter pCycRate  as decimal no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pHourRate <> 0 and pHourRate <> ?
      then do:
         assign
            pRun     = 1 / pHourRate
            pCycRate = pHourRate.
      end. /* then do: */
      else do:
         assign
            pRun     = 0
            pCycRate = 0.
      end. /* else do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* calculateCycleRate */

/*============================================================================*/
PROCEDURE validateStart :
/*------------------------------------------------------------------------------
Purpose:      Validate Start Date of Routing Operation
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post: Validated Start Date
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pStartDate as date no-undo.
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if ro_start <> pStartDate and
         can-find(first ro_det
                  where recid(ro_det) <> recid(ro_det)
                  and   ro_routing    =  ro_routing
                  and   ro_op         =  ro_op
                  and   ro_start      =  pStartDate )
      then do:
         /* MESSAGE #122 - DATE RANGES MAY NOT OVERLAP */
         {pxmsg.i &MSGNUM=122
                  &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         return error {&APP-ERROR-RESULT}.
      end. /* then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateStart */

/*============================================================================*/
PROCEDURE validateStartDateRange :
/*------------------------------------------------------------------------------
Purpose:      Validate overlap of Date Range with existing Routing Operation
              based on the entered Start Date
Exceptions:   WARNING-RESULT
Conditions:
        Pre:  ro_det(r)
        Post:
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pStartDate as date no-undo.
   define parameter buffer ro_det for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      define variable conflicts as logical no-undo.
      define buffer rodet for ro_det.

      /* THIS SECTION VALIDATES THAT THE DATE BEING ENTERED DOES NOT FALL */
      /* INSIDE OF THE DATE RANGE OF AN EXISTING RECORD.                  */
      conflicts = false.
      for each rodet
         fields(ro_routing ro_op ro_start ro_end)
         where rodet.ro_routing = ro_det.ro_routing
         and   rodet.ro_op      = ro_det.ro_op
         and   recid(ro_det)   <> recid(rodet)
         and   ((rodet.ro_start = ? and pStartDate   = ?)
         or     (rodet.ro_start = ? and rodet.ro_end = ?)
         or     ((pStartDate >= rodet.ro_start or rodet.ro_start = ?)
         and      pStartDate <= rodet.ro_end))
         no-lock:
         conflicts = true.
         leave.
      end. /* for each rodet no-lock */

      if conflicts = true
      then do:
         /* MESSAGE #122 - DATE RANGES MAY NOT OVERLAP */
         {pxmsg.i &MSGNUM=122
                  &ERRORLEVEL={&WARNING-RESULT}
         }
         return {&WARNING-RESULT}.
      end.  /* if conflicts */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateStartDateRange */

/*============================================================================*/
PROCEDURE validateYield :
/*------------------------------------------------------------------------------
Purpose:      Validate the Yield Percentage of the Routing Operation
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  ro_det(r)
        Post: Validated Yield Percentage
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pYield as decimal no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pYield = 0
      then do:
         /* MESSAGE #3953 - VALUE MUST BE GREATER THAN ZERO */
         {pxmsg.i &MSGNUM=3953
                  &ERRORLEVEL={&APP-ERROR-RESULT}
         }
         return error {&APP-ERROR-RESULT}.
      end. /* then do: */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateYield */

/*============================================================================*/
PROCEDURE validateWorkCenter :
/*------------------------------------------------------------------------------
Purpose:      Implement schema validation of Routing Work Center (ro_wkctr)
Exceptions:   APP-ERROR-RESULT
Conditions:
        Pre:  Entry of Work Center
        Post: Validated Work Center
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pWorkCenter as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if not(can-find(first wc_mstr where wc_wkctr = pWorkCenter))
      then do:
         /* MESSAGE #739 - WORK CENTER NOT DEFINED WITH THIS CODE */
         {pxmsg.i &MSGNUM=739
                  &ERRORLEVEL={&APP-ERROR-RESULT}
                  &FIELDNAME=""ro_wkctr""
         }
         return error {&APP-ERROR-RESULT}.
      end. /* if not(can-find(first wc_mstr... */
   end.
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateWorkCenter */

/*============================================================================*/
PROCEDURE validateToolCode :
/*------------------------------------------------------------------------------
Purpose:      Implement schema validation of Routing Tool Code (ro_tool)
Exceptions:   NONE
Conditions:
        Pre:  Entry of Tool Code
        Post: Validated Tool Code
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pToolCode as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC='validateGeneralizedCodes' &PROGRAM='gpcodxr.p'
                &HANDLE=ph_gpcodxr
               &PARAM="(input 'ro_tool',
                       input '',
                       input pToolCode,
                       input '')"
               &FIELD-LIST=ro_det.ro_tool}
      if return-value <> {&SUCCESS-RESULT} then
         return error return-value.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateToolCode */

/*============================================================================*/
PROCEDURE validateWrite :
/*------------------------------------------------------------------------------
Purpose:      Validate Routing Operation Detail Table
Exceptions:   NONE
Conditions:
        Pre:  ro_det(w)
        Post: NONE
Notes:        This procedure is called by the Routing Operation DTC to
              perform the schema validations for the ro_det fields displayed
              in the application.
History:
------------------------------------------------------------------------------*/
   define parameter buffer pRoutingBuffer for ro_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      {pxrun.i &PROC = 'validateWorkCenter'
               &PARAM = "(input pRoutingBuffer.ro_wkctr)"}
      if return-value <> {&SUCCESS-RESULT} then
         return error return-value.

      {pxrun.i &PROC = 'validateToolCode'
               &PARAM = "(input pRoutingBuffer.ro_tool)"}
      if return-value <> {&SUCCESS-RESULT} then
         return error return-value.
   end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
   return {&SUCCESS-RESULT}.
END PROCEDURE. /* validateWrite */
